import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { getAllItfCateList } from '/@/api/sys/auth';
import { InterfaceCateListGetResultModel } from '/@/api/sys/model/authModel';

let list: InterfaceCateListGetResultModel | null;

export const getCateOptions = async () => {
  if (!list) {
    list = await getAllItfCateList();
  }
  return list;
};

export const columns: BasicColumn[] = [
  {
    title: '接口名称',
    dataIndex: 'name',
    width: 160,
    align: 'left',
  },
  {
    title: '接口路径',
    dataIndex: 'path',
    width: 250,
  },
  {
    title: '系统类型',
    dataIndex: 'type',
    width: 100,
  },
  {
    title: '接口类型',
    dataIndex: 'category',
    width: 80,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 50,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ record }) => {
      const status = record.status;
      const enable = ~~status === 0;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, { color: color }, () => text);
    },
  },

  {
    title: '备注',
    dataIndex: 'remark',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    width: 180,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '接口名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: false },
        { label: '停用', value: true },
      ],
    },
    colProps: { span: 8 },
  },
  {
    field: 'categoryId',
    label: '接口类型',
    component: 'ApiSelect',
    componentProps: {
      api: getAllItfCateList,
      labelField: 'name',
      valueField: 'id',
    },
    colProps: { span: 8 },
  },
  {
    field: 'type',
    label: '系统类型',
    component: 'Select',
    componentProps: {
      options: [
        { label: '后端管理', value: 'dashboard' },
        { label: '门户网站', value: 'portal' },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: '',
    component: 'InputNumber',
    show: false,
  },
  {
    field: 'name',
    label: '接口名称',
    component: 'Input',
    required: true,
    colProps: { span: 24 },
  },
  {
    field: 'path',
    label: '接口路径',
    component: 'Input',
    required: true,
    colProps: { span: 24 },
  },
  {
    field: 'type',
    label: '系统类型',
    component: 'Select',
    required: true,
    colProps: { span: 24 },
    componentProps: {
      options: [
        { label: '后端管理', value: 'dashboard' },
        { label: '门户网站', value: 'portal' },
        { label: '其他', value: 'other' },
      ],
    },
  },
  {
    field: 'categoryId',
    label: '接口类型',
    component: 'ApiSelect',
    required: true,
    colProps: { span: 24 },
    componentProps: {
      api: getAllItfCateList,
      labelField: 'name',
      valueField: 'id',
    },
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    colProps: { span: 24 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: false,
    colProps: { span: 24 },
    componentProps: {
      options: [
        { label: '启用', value: false },
        { label: '停用', value: true },
      ],
    },
    required: true,
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    colProps: { span: 24 },
  },
];
